package com.djc8.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.djc8.entity.BookBean;
import com.djc8.entity.Page;
import com.djc8.entity.UserInfoBean;
import com.djc8.model.Model;

/**
 * Servlet implementation class GetBookList
 */
public class GetBookList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String page=request.getParameter("page");
		
		String row=request.getParameter("rows");
		
		Model model=new Model();
		Page p=null;
		if(page!=null && row!=null && !page.equals("") && !row.equals("") )
		{
			p=model.doPageForBook(Integer.parseInt(page), Integer.parseInt(row));
		}
		else
		{
			p=model.doPageForBook(1, 10);
		}
		
		List<BookBean> list=new ArrayList<BookBean>(p.getPageList());
		
		JSONArray jsonArray=new JSONArray();
		for (BookBean bean : list) {
			
			JSONObject obj=new JSONObject();
			obj.put("bookID",bean.getBookInfoBean().getBookID() );
			obj.put("bookName",bean.getBookInfoBean().getBookName());
			obj.put("bookAuthor", bean.getBookDetialBean().getBookAuthor());
			obj.put("bookPub", bean.getBookDetialBean().getBookPub());
			obj.put("bookPubDate", bean.getBookDetialBean().getBookPubDate().toString());
			obj.put("bookPage", bean.getBookDetialBean().getBookPage());
			obj.put("bookISBN", bean.getBookDetialBean().getBookISBN());
			obj.put("bookAddDate", bean.getBookDetialBean().getBookAddDate().toString());
			obj.put("bookContent",bean.getBookDetialAddBean().getBookContent());
			obj.put("bookCatalogue", bean.getBookDetialAddBean().getBookCatalogue());
			jsonArray.add(obj);
		}
		JSONObject obj=new JSONObject();
		obj.put("total", p.getTotalNum());
		obj.put("rows", jsonArray);
		
		//jsonArray.add(new UserInfoBean(0,0,"1","1","2","3","4",null,null));
		
		response.getWriter().print(obj.toString());
		//System.out.println(jsonArray.toString());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
